var api = require('../../utils/api.js');


Page({
    /**
     * 页面的初始数据
     */
    data: {
        lists0:[],
        lists1:[],
        twopid:0,
        pid1:0,
        carnumber:0,
        key:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getcatesbypid0();
    },


    //点击一级分类时
    getonelist(ret){
        console.log(ret.currentTarget.dataset.id);
        var id=ret.currentTarget.dataset.id;

        this.setData({
            pid1:id,
            goods:[],
            title:'',
            lists1:[]
        })

        this.getcatesbypid1();
    },

    gettowlist(ret){
        console.log(ret.currentTarget.dataset.id);
        var id=ret.currentTarget.dataset.id;

        this.setData({
            twopid:id,
            goods:[],
            title:'',
        })

        this.getgoodbypid();
    },

    getlistbykey(ret){

        this.setData({
            key:ret.detail.value,
            goods:[]
        })

        this.getcatesbypid1();

    },

    seeinfo(ret){

        var id=ret.currentTarget.dataset.id;
        wx.navigateTo({
          url: '/pages/car/info?id='+id
        })
    },
    seeall(){
        wx.navigateTo({
            url: '/pages/car/all',
          })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
       this.getcarcount();
    },

    //购物车数量
    getcarcount(){
        var that=this;
        api.post({
            url: 'cars/getcarcount',
            data: {},
            success: data => {
                that.setData({
                    carnumber:data.data
                })
              
            }
        })

    },
    //获取分类产品
    getgoodbypid(){
        var that=this;
        var id=this.data.twopid;
        var key=this.data.key;

        api.post({
            url: 'cars/getgoodbypid',
            data: {pid:id,key:key},
            success: data => {
                that.setData({
                    goods:data.data.lists,
                    title:data.data.title
                })
                console.log(data);
              
            }
        })
    },

    // 获取分类信息
    getcatesbypid0(){
        var that=this;

        api.post({
            url: 'cars/getallcate',
            data: {pid:0},
            success: data => {

                var list=data.data;


                that.setData({
                    lists0:data.data,
                    pid1:list[0].id
                })

                console.log(list[0].id);
                that.getcatesbypid1();
            }
        })
    },

       // 获取分类信息
    getcatesbypid1(){
        var that=this;
        var pid=that.data.pid1;

        api.post({
            url: 'cars/getallcate',
            data: {pid:pid},
            success: data => {

                var list=data.data;

                that.setData({
                    lists1:data.data,
                    twopid:list[0].id
                })

                that.getgoodbypid();
                console.log(data);
              
            }
        })
    }

})